package com.example.phone.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Gallery.LayoutParams;

public class ListBusActivity extends Activity {

	// private List<String> data = new ArrayList<String>();
	private ListView listBus;
	private List<Map<String, Object>> listItems;
	private DisplayMetrics dm;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		setContentView(R.layout.list_bus_activity);
		listBus = (ListView) findViewById(R.id.listBus);
		listItems = getData();
		ListViewAdapter adapter = new ListViewAdapter(this);
		listBus.setAdapter(adapter);
		Spinner spin = (Spinner) findViewById(R.id.busByType);
		ArrayAdapter<CharSequence> adapters = ArrayAdapter.createFromResource(
				this, R.array.busType_arry,
				android.R.layout.simple_spinner_item);
		adapters.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adapters);
		Spinner spin1 = (Spinner) findViewById(R.id.paixu);
		ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
				this, R.array.paixu_arry, android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin1.setAdapter(adapter1);
		LinearLayout.LayoutParams sp_params = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		sp_params.height = dm.widthPixels * 2 / 16;
		spin.setLayoutParams(sp_params);
		spin1.setLayoutParams(sp_params);

	}

	private List<Map<String, Object>> getData() {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("title", "焦作三岛输送机械有限公司");

		map.put("info", "汽车司机");

		map.put("img", R.drawable.distance);
		map.put("time", "2013-01-12 06:22:43");
		map.put("number", "3");
		map.put("pay", "2500");

		list.add(map);

		map = new HashMap<String, Object>();

		map.put("title", "焦作市福得机械厂");

		map.put("info", "机械工程师");

		map.put("img", R.drawable.level);
		map.put("time", "2013-03-12 12:52:03");
		map.put("number", "5");
		map.put("pay", "5500");

		list.add(map);

		map = new HashMap<String, Object>();

		map.put("title", "焦作市滨湖生活广场");

		map.put("info", "商城服务员");

		map.put("img", R.drawable.music);
		map.put("time", "2012-01-12 16:52:43");
		map.put("number", "20");
		map.put("pay", "3200");

		list.add(map);

		map = new HashMap<String, Object>();

		map.put("title", "焦作市三鑫纸业有限公司");

		map.put("info", "造纸工人");

		map.put("img", R.drawable.name);
		map.put("time", "2011-01-30 23:22:43");
		map.put("number", "3");
		map.put("pay", "2600");

		list.add(map);

		return list;

	}

	static class ListItemView {

		public ImageView img;
		public TextView title;
		public TextView info;
		public TextView time;
		public TextView number;
		public TextView pay;

	}

	public class ListViewAdapter extends BaseAdapter {

		private LayoutInflater listContainer;

		public ListViewAdapter(Context context) {
			listContainer = LayoutInflater.from(context);

		}

		@Override
		public int getCount() {

			return listItems.size();

		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Map<String, Object> map = (Map<String, Object>) listItems
					.get(position);
			ListItemView holder = null;
			// 如果缓存convertView为空，则需要创建View
			if (convertView == null) {
				holder = new ListItemView();
				// 根据自定义的Item布局加载布局
				convertView = listContainer.inflate(R.layout.one_bus, null);
				holder.img = (ImageView) convertView.findViewById(R.id.img);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.info = (TextView) convertView.findViewById(R.id.info);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.pay = (TextView) convertView.findViewById(R.id.pay);
				// 将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
				convertView.setTag(holder);
			} else {
				holder = (ListItemView) convertView.getTag();
			}
			holder.img.setBackgroundResource((Integer) map.get("img"));
			holder.title.setText((String) map.get("title"));
			holder.info.setText((String) map.get("info"));
			holder.number.setText((String) map.get("number"));
			holder.time.setText((String) map.get("time"));
			holder.pay.setText((String) map.get("pay"));
			return convertView;
		}
	}

}
